package top.lshaci.learning.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * HeartBeatServerHandler
 *
 * @author lshaci
 * @since 1.0.0
 */
@Slf4j
public class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;

            IdleState state = event.state();
            if (state == null) {
                return;
            }
            log.warn("[客户端] {}, {} --超时时间--", ctx.channel().remoteAddress(), state);
            // 发生空闲，关闭通道
            ctx.close();

        }

//        if (evt instanceof IdleStateEvent) {
//            IdleStateEvent e = (IdleStateEvent) evt;
//            if (e.state() == IdleState.READER_IDLE) {
//                ctx.close();
//            } else if (e.state() == IdleState.WRITER_IDLE) {
//                ctx.writeAndFlush(new PingMessage());
//            }
//        }
    }

}
